import { defineStore } from 'pinia'

interface ITabsNav {
    path: string,
    title: string,
    active?: boolean, // 是否为激活项
}

// tabs-nav 仓库
const useTabsStore = defineStore('tabs', {
    state(){
        return{
            // 保存所有的路由标签页
            tabs: [
                {
                    path: '/dashboard',
                    title: '仪表盘',
                    active: true,
                },
            ],
        }
    },
    actions: {
        // 添加访问路由标签页
        addTabs(tab: ITabsNav){
            // 将所有激活字段设置为false
            this.tabs.map(t => {
                t.active = false
                return t
            })
            const has = this.tabs.find(t => t.path === tab.path)
            // 如果不相同则添加到数组中
            if(!has){
                this.tabs.push({
                    ...tab,
                    active: true,
                })
            }else{
                has.active = true
            }
        },
        // 删除访问路由标签页
        removeTabs(tab: string){
            this.tabs = this.tabs.filter(t => t.path !== tab)
        },

        // 下拉菜单
        removeCommand(command: string) {
            switch (command) {
                case 'current':
                    this.tabs = this.tabs.filter(tab => !tab.active)
                    break;
                case 'other':
                    this.tabs = this.tabs.filter((tab, index) => {
                        if(index === 0){
                            return true
                        }
                        return tab.active
                    })
                    break;
                case 'all':
                    this.tabs.splice(1)
                    break;
            }
        },

    },
    persist: {
        storage: sessionStorage,
    }
})


export default useTabsStore